<template>
	<view class="order">
		<view class="items" v-if="!showLoading">
			<view class="item" @click="toEventDetail(info.eventInfo.eventId)">
				<view class="title">
					{{info.eventInfo.eventName}}
				</view>
				<view class="text">
					研学时间：{{info.eventInfo.time}}
				</view>
				<view class="text">
					研学地址：{{info.eventInfo.address}}
				</view>
				<view class="text">
					研学人数：{{info.eventInfo.studentCount}}人
				</view>
				<view class="text">
					研学费用：{{info.eventInfo.totalPrice}}元
				</view>
			</view>
		</view>
		<view class="cost" v-else>
			<view class="cost-title">
				研学活动
			</view>
			<u-skeleton rows="4" titleWidth="100%" rowsWidth="60%" title :loading="showLoading"></u-skeleton>
		</view>
		<view class="student" v-if="!showLoading && info.studentList.length">
			<view class="student-title">
				研学人({{info.orderInfo.studentCount}})
			</view>
			<view class="student-item" v-for="(item, index) in info.studentList" :key="index">
				<view class="name">
					{{item.name}}
				</view>
				<view class="idcard">
					{{item.idCard | esensitizeIdCard}}
				</view>
				<view class="check-txt" v-if="info.orderStatus === 'VERIFY'">
					使用身份证 现场核销
				</view>
			</view>
		</view>
		<view class="cost" v-if="showLoading">
			<view class="cost-title">
				研学人
			</view>
			<u-skeleton rows="1" titleWidth="20%" rowsWidth="60%" title :loading="showLoading"></u-skeleton>
		</view>
		<view class="cost" v-if="!showLoading">
			<view class="cost-title">
				费用清单
			</view>
			<view class="order-num">订单编号：{{info.orderInfo.orderNum}}</view>
			<view class="cost-item">
				<view class="cost-label">
					研学人数:
				</view>
				<view class="cost-value">
					共计<text>{{info.orderInfo.studentCount}}</text>人
				</view>
			</view>
			<view class="cost-item">
				<view class="cost-label">
					研学费用:
				</view>
				<view class="cost-value">
					<view class="">{{info.orderInfo.unitPrice}}元/人</view>
					<view class="">共计{{info.orderInfo.totalPrice}}元</view>
				</view>
			</view>
			<view class="cost-item">
				<view class="cost-label">
					折扣:
				</view>
				<view class="cost-value">
					{{info.orderInfo.discount}}
				</view>
			</view>
			<view class="cost-fee" v-if="info.orderStatus !== 'CANCEL'">
				{{info.orderStatus === 'UNPAID' ? '共需支付' : '已支付'}}<text>{{info.orderInfo.totalPrice}}</text>元
			</view>
			<view class="cost-fee" v-else>
				<text>已取消</text>
			</view>
		</view>
		<view class="cost" v-else>
			<view class="cost-title">
				费用清单
			</view>
			<u-skeleton rows="3" titleWidth="40%" rowsWidth="60%" title :loading="showLoading"></u-skeleton>
		</view>

		<view class="comment-box" v-if="info.orderStatus === 'COMPLETE'">
			<CommentBox :orderNum="info.id"></CommentBox>
		</view>
		
		<view class="footer" v-if="!showLoading">
			<view class="btn-two" v-if="info.orderStatus === 'UNPAID'">
				<view class="btn-left" @click="cancel">
					取消订单
				</view>
				<view class="btn-right" @click="pay">
					去支付
				</view>
			</view>
			<view class="cancel-btn" v-else-if="info.orderStatus === 'VERIFY'" @click="cancel">
				取消报名
			</view>
			<view class="comment-btn" v-else-if="info.orderStatus === 'COMMENT'" @click="toComment">
				去评价
			</view>
		</view>
	</view>
</template>

<script>
	import CommentBox from './components/comment-box.vue';
	import {
		orderTabs
	} from '@/utils/contant.js';
	import {
		esensitize
	} from '@/utils/index.js';

	export default {
		data() {
			return {
				showLoading: true,
				orderId: '',
				info: {
					orderStatus: '',
				},
			}
		},
		onLoad(options) {
			this.orderId = options.id;
			this.getOrderInfo(this.orderId);
		},
		onShow() {
			this.getOrderInfo(this.orderId);
		},
		onPullDownRefresh() {
			this.getOrderInfo(this.orderId);
			uni.stopPullDownRefresh();
		},
		filters: {
			esensitizeIdCard(val) {
				return esensitize(val, 6, 8);
			}
		},
		methods: {
			async getOrderInfo(id) {
				const res = await this.$api('/study/mobile/personal/center/order/info', 'get', {
					id
				});
				this.showLoading = false;
				this.info = res.data;
				this.$forceUpdate();
			},
			back() {
				uni.navigateBack();
			},
			cancel() {
				const _this = this;
				uni.showModal({
					content: '确定取消订单',
					showCancel: true,
					success: async function(res) {
						if (res.confirm) {
							const res = await _this.$api('/study/mobile/event/sign/cancel', 'get', {
								id: _this.orderId
							});
							if(res.code === 0) {
								uni.showToast({
									title: '取消成功'
								})
								_this.getOrderInfo(_this.orderId);
							}
						}
					}
				})
			},
			async pay() {
				uni.showLoading({
					title: '支付中...'
				});
				const res = await this.$api('/study/pay/payment', 'post', {
					orderNum: this.info.orderInfo.orderNum,
					payType: 'WECHAT_PAY',
				});
				uni.hideLoading();
				if(res.code===0) {
					uni.showToast({
						title: '支付成功',
					});
					this.getOrderInfo(this.orderId);
				}
			},
			toComment() {
				uni.navigateTo({
					url: `/pages/my/comment-create?id=${this.info.id}`
				});
			},
			toEventDetail(id) {
				uni.navigateTo({
					url: `/pages/study/event-detail?id=${id}`
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.order {
		margin: 0 30rpx 30rpx;
		padding-bottom: 180rpx;
		
		.skeleton {
			background-color: #fff;
			padding: 30rpx;
			border-radius: 20rpx;
			margin: 20rpx 0;
		}

		.items {
			.item {
				margin-top: 30rpx;
				padding: 30rpx;
				background-color: #fff;
				border-radius: 20rpx;

				.title {
					padding-bottom: 20rpx;
					margin-bottom: 20rpx;
					color: #218BCC;
					font-size: 36rpx;
					border-bottom: 1rpx solid #ccc;
				}

				.text {
					padding: 10rpx 0;
					color: #203F3F;
				}
			}
		}

		.student {
			margin-top: 30rpx;
			padding: 30rpx;
			background-color: #fff;
			border-radius: 20rpx;

			.student-title {
				padding-bottom: 20rpx;
				margin-bottom: 20rpx;
				border-bottom: 1rpx solid #ccc;
			}

			.student-item {
				margin: 20rpx 0;

				.idcard {
					color: #999;
				}
				
				.check-txt {
					font-size: 28rpx;
					padding-top: 10rpx;
					color: #74BB2A;
				}
			}
		}

		.cost {
			margin-top: 30rpx;
			padding: 30rpx;
			background-color: #fff;
			border-radius: 20rpx;

			.cost-title {
				display: flex;
				flex-direction: row;
				align-items: center;
				padding-bottom: 20rpx;
				margin-bottom: 20rpx;
				border-bottom: 1rpx solid #ccc;
			}
			
			.order-num {
				color: #666;
				font-size: 28rpx;
			}

			.cost-item {
				display: flex;
				flex-direction: row;
				margin: 20rpx 0;

				.cost-label {
					width: 130rpx;
					font-size: 28rpx;
				}

				.cost-value {
					display: inline-block;

					text {
						color: #74BB2A;
					}
				}
			}

			.cost-fee {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: flex-end;

				text {
					padding: 0 10rpx 4rpx;
					font-size: 40rpx;
					color: #74BB2A;
				}
			}
		}
		
		.footer {
			.cancel-btn {
				width: 100%;
				height: 120rpx;
				line-height: 120rpx;
				text-align: center;
				border-top: 1px solid #D7A768;
				color: #D7A768;
			}
			
			.comment-btn {
				width: 100%;
				height: 120rpx;
				line-height: 120rpx;
				background-color: #74BB2A;
				text-align: center;
				color: #fff;
			}
		}
	}
</style>